package com.house.internal.controllers;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.house.core.model.Employees;
import com.house.core.model.ErrorCode;
import com.house.core.model.response.LoginResponse;
import com.house.core.model.response.ValidateUsernameResponse;
import com.house.core.service.EmployeesService;

/**
 *
 *
 * @author <a href="mailto:chong.sun@renren-inc.com">sun chong</a>
 * @version 2013-2-19
 */

@Controller
@RequestMapping("/user")
public class UserController {

//	private DESEncryption des = new DESEncryption(Const.DES_ID_PASSWORD_KEY);
	
	@Resource(type = EmployeesService.class)
	private EmployeesService employeesService;
	
	/**
	 * 登录时uid和username至少传一个，如果有uid则优先使用uid登录，uid为null使用username登录
	 * @param request
	 * @param uid
	 * @param username
	 * @param password
	 * @param loginExpire
	 * @return
	 */
	@RequestMapping("/landing")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
			ModelAndView mv = new ModelAndView("login");
			return mv;
		}
		
	
	@ResponseBody
	@RequestMapping("/login")
	public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response, 
			@RequestParam(value = "accountId") String accountId, 
			@RequestParam("password") String password, 
			@RequestParam(value = "login_expiry", defaultValue = "86400") Integer loginExpire) {
		
		boolean isNum = this.isCanParseNum(accountId);
		Long uid = null;
		String username = null;
		if(isNum){
			uid = Long.parseLong(accountId);
		}else{
			username = accountId;
		}
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		LoginResponse resp = employeesService.login(uid, username, password);
		modelMap.put("errorCode", resp.getErrorCode());		
		//响应成功
		if(resp.getErrorCode() == ErrorCode.SUCCESS){
			Employees employees = resp.getEmployees();
			
			//判断员工是否有登录权限
			if(employees.canLogin()){
				//将username password写到cookie中
//				Cookie sessionKeyCookie = new Cookie(Const.ID_PASSWORD, des.encryptStr(employees.getId() + ";" + employees.getPassword()));
//				sessionKeyCookie.setPath("/");
//				sessionKeyCookie.setMaxAge(loginExpire);
//				response.addCookie(sessionKeyCookie);
				
				//将员工对象放到session中
				request.getSession().setAttribute("employees", employees);
				modelMap.put("employees", employees);
			}
		}
		return modelMap;
	}
	
	@ResponseBody
	@RequestMapping("/validate_username")
	public Map<String, Object> validateUsername(@RequestParam(value = "username") String username){
		ValidateUsernameResponse resp = employeesService.validateUsername(username);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("errorCode", resp.getErrorCode());
		modelMap.put("recommendNum", resp.getRecommendNum());
		return modelMap;
	}
	
	
	
	public boolean isCanParseNum(String str){
	    if(str == null){//非空验证
	        return false;
	    }
	    return str.matches("\\d+");//正则表达式判断该字串值是否为数字
	}
}
